package com.youyou.action;
import com.youyou.entity.Account;
import com.youyou.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


@Controller
@SessionAttributes("account")
public class AccountAction {
    @Autowired
    private AccountService accountService;

    @ModelAttribute("account")
    Account getAccount() {
        return null;
    }

    //0.首页
    @RequestMapping("/index")
    public String index(@ModelAttribute("account") Account account,Model model){
        if(account==null){
            model.addAttribute("message","LOGIN_TIME_OUT");
            return "redirect:loginView";
        }
        return "index";
    }


    //2.查询余额
    @RequestMapping("/selectAccount")
    public String queryBanlance(@ModelAttribute("account") Account account,Model model) {
        if (account == null) {
            model.addAttribute("message", "LOGIN_ERROR");
            return "redirect:loginView";
        }
        Integer balance = accountService.getBalanceById(account.getId());
        model.addAttribute("balance", balance);
        return "selectAccount";
    }


    //3.1 修改密码的页面
    @RequestMapping("/updatePassword")
    public String updatePassword(@ModelAttribute("account") Account account,Model model){
        if (account==null){

            model.addAttribute("message","LOGIN_ERROR");
            return "redirect:loginView";

        }
        return "updatepassword";
    }    //修改密码的action
    @RequestMapping("/updatePasswordaction")
    public String updatePassword(@ModelAttribute("account") Account account,Model model,String newPassword,
                                 String oldPassword,String newPassword2){
        if (account==null){
            model.addAttribute("message","LOGIN_ERROR");
            return "redirect:loginView";
        }
        if (newPassword.equals(oldPassword)){
            model.addAttribute("message","OLD_EQUAL_NEW");
            return "redirect:updatepassword";
        }

        if (newPassword.length()>6){
            model.addAttribute("message","PASSWORD_LONG");
            return "redirect:updatepassword";
        }
        boolean updatepassword=accountService.updatepassword(newPassword,oldPassword,account.getId());
        if (updatepassword){
            return "updatepassword_success";
        }
        return "updatepassword_Errer";
    }
    //取钱
    @RequestMapping("/getmoney")
    public String  getmoney(@ ModelAttribute("account") Account account,Model model){
        if (account==null){
         model.addAttribute("message","LOGIN_ERROR");
            return "redirect:logView";
        }
        return "getmoney";
    }

    @RequestMapping("/getmoneyaction")
    public  String getmoneyaction(@ModelAttribute("account") Account account,Model model,Integer money){
        System.out.println(account.getId()+money);
        if (account==null){
        model.addAttribute("message","LOGIN_ERROR");
        return "redirect:loginView";
    }
    boolean getmoneyaction=accountService.getMoney(money,account.getId());

        if (getmoneyaction){
            model.addAttribute("money",money);
            return "getmoney_success";
        }else{
                return "getmoney_error";
        }
    }
    //存钱
    @RequestMapping("/savemoney")
    public String savemoney(@ModelAttribute("account") Account account,Model model){
       if (account==null){
           model.addAttribute("message","LOGIN_ERROR");
           return "redirect:loginView";
       };

        return "savemoney";
}
    @RequestMapping("/savemoneyaction")
    public  String savemoneyaction(@ModelAttribute("account")Account account,Model model,Integer money) {
    if (account == null) {
        model.addAttribute("message", "LOGIN_ERROR");
    }
    boolean saveMoney = accountService.saveMoney(money, account.getId());
    System.out.println(account.getId()+"-------"+account.getBalance()+money);
    if (saveMoney) {
        model.addAttribute("money",money);
        return "savemoney_success";
    }
        return "savemoney_error";
    }
    //转账
    @RequestMapping("/transmoney")
    public  String tranmoney(@ModelAttribute("account") Account account,Model model){
    if (account==null){
        model.addAttribute("message", "LOGIN_ERROR");
    }
        return "transmoney";
    }
    @RequestMapping("/transmoneyaction")
    public  String transmoneyaction(@ModelAttribute("account") Account account,Model model,Integer money,Integer userIdDest){
        if (account==null){
            model.addAttribute("message", "LOGIN_ERROR");
            return "redirect:loginView";
        }
        System.out.println(money+"--------------"+userIdDest);
        boolean transMoney = accountService.transMoney(money,userIdDest,account.getId());
        if (transMoney){
            return "transmoney_success";
        }else{
            return "transmoney_error";
        }
    }

}
